﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;


public class InitCar : MonoBehaviour {
    public GameObject Point1;
    public GameObject Point2;
    public GameObject Point3;
    public GameObject Point4;
    public GameObject snow;
    public GameObject rain;
    private CarControl carControl1;
    private CarControl carControl2;
    private GameObject car1;
    private GameObject car2;
    public GameObject maincamera;
    private string WeatherType;
    private string cartype1;
    private string cartype2;
    private float CarMass1;
    private float CarMass2;
    private float FirstSpeed;
    private float SeconedSpeed;
    private string CollectType;
    private CarAIControl Car1AI;
    private PhysicMaterial roadFriction;
    private float accelerates1;//加速度计算采用公式是 （扭矩/车轮半径-摩擦阻力）
    private float accelerates2;
    private CarAIControl Car2AI;
    private float x;
    Damage a;
    Damage b;
    //private string WeatherType;
	// Use this for initialization
    void Awake()
    {
        choose();

	}
	
	// Update is called once per frame
	void Update () {
        if (carControl1.CurrentSpeed >= (FirstSpeed-2) && carControl2.CurrentSpeed >= (SeconedSpeed-2) && CollectType == "逆向")
        {

            Car1AI.SetTarget(car2.transform);
            Car2AI.SetTarget(car1.transform);
        }
        if (carControl1.CurrentSpeed >= (FirstSpeed - 2) && carControl2.CurrentSpeed >= (SeconedSpeed - 2) && CollectType == "同向")
        {

            Car1AI.SetTarget(car2.transform);
            Car2AI.SetTarget(car1.transform);
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            a.autoBounce=true;
            b.autoBounce = true;
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            SceneManager.LoadScene("Sence/cae1");
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene("Sence/cae2");
        }
	}
    void getdate() {
        FirstSpeed = PlayerPrefs.GetFloat("sudu1");
        SeconedSpeed = PlayerPrefs.GetFloat("sudu2");
        CarMass1 = PlayerPrefs.GetFloat("zhiliang1");
        CarMass2 = PlayerPrefs.GetFloat("zhiliang2");
        CollectType = PlayerPrefs.GetString("leixing").Trim();
        cartype1 = PlayerPrefs.GetString("leixing1");
        cartype2 = PlayerPrefs.GetString("leixing2");
        WeatherType = PlayerPrefs.GetString("weather").Trim();
        roadFriction = Resources.Load("Prefabs/InitPoint/Road") as PhysicMaterial;//得到接触面的材质
        accelerates1 = ((6000 / 0.315f) - (CarMass1 + 80 + 400) * roadFriction.dynamicFriction) / (CarMass1 + 80 + 400);
        accelerates2 = ((6000 / 0.315f) - (CarMass2 + 80 + 400) * roadFriction.dynamicFriction)/ (CarMass2 + 80 + 400);
        float s1 = Mathf.Pow(FirstSpeed / accelerates1, 2) * 0.5f * accelerates1;
        float s2 = Mathf.Pow(SeconedSpeed / accelerates2, 2) * 0.5f * accelerates2;
        x = Mathf.Abs(s1 - s2)*0.06f;
        Debug.Log(WeatherType);
    }
    void setcar() {

        if (cartype1 == "轿车")
        {
            car1 =  Resources.Load("Prefabs/Vehicles/FamilyCar") as GameObject;
        }
        else if (cartype1 == "跑车")
        {
            car1 = Resources.Load("Prefabs/Vehicles/SportsCar") as GameObject;
        }
        else if (cartype1 == "货车")
        {
            car1 = Resources.Load("Prefabs/Vehicles/Truck") as GameObject;
        }

        if (cartype2 == "轿车")
        {

            car2 = Resources.Load("Prefabs/Vehicles/FamilyCar") as GameObject;
        }
        else if (cartype2 == "跑车")
        {
            car2 = Resources.Load("Prefabs/Vehicles/SportsCar") as GameObject;
        }
        else if (cartype2 == "货车")
        {
            car2 = Resources.Load("Prefabs/Vehicles/Truck") as GameObject;
        }
    }
    //设置天气
    void setWeather()
    {
        rain.SetActive(false);
        snow.SetActive(false);
        if (WeatherType == "雨天")
        {
            rain.SetActive(true);
            roadFriction.dynamicFriction = 0.20f;//设置摩擦
        }
        else if (WeatherType == "雪天")
        {
            snow.SetActive(true);
            roadFriction.dynamicFriction= 0.15f;//设置摩擦
        }
    }

    //设置碰撞类型
    void setColliderType()
    {
        if (CollectType == "正碰")
        {
            Point1.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
            Point1.transform.position = new Vector3(186, 1, 2);
            Point2.transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
            Point2.transform.position = new Vector3(-186, 1, 2);
        }
        if (CollectType == "追尾")
        {
            Point1.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
            Point1.transform.position = new Vector3(186, 1, 2);
            Point2.transform.position = new Vector3(60, 1, 2);
            Point2.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
            Point3.transform.position = new Vector3(-186, 1, 2);

        }
        if (CollectType == "同向")
        {
            Point1.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
            Point1.transform.position = new Vector3(186, 1, -3);
            Point2.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
            Point2.transform.position = new Vector3(186, 1, 3);
            /* if (FirstSpeed < SeconedSpeed)
              {
                  Point1.transform.position = new Vector3(186-x, 1, -3);

              }
              else
              {
                  Point2.transform.position = new Vector3(186 - x, 1, 3);
              }*/
            
              Point3.transform.position = new Vector3(-186, 1, -3);
              Point4.transform.position = new Vector3(-186, 1,3);
          }
            if (CollectType == "逆向")
            {
                Point1.transform.rotation = Quaternion.Euler(new Vector3(0, -90, 0));
                Point1.transform.position = new Vector3(186, 1, 3);
                Point3.transform.position = new Vector3(-186, 1, 3);
                Point2.transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
                Point2.transform.position = new Vector3(-186, 1, -3);
                Point4.transform.position = new Vector3(186, 1, -3);
            }
        
    }
    void choose()
    {
        getdate();//得到数据
        setcar();//设置车型
        setWeather();//设置天气
        setColliderType();//设置碰撞类型
        car1 = Instantiate(car1, Point1.transform.localPosition, Point1.transform.rotation);//初始化车一
        car2 = Instantiate(car2, Point2.transform.localPosition, Point2.transform.rotation);//初始化车二
        carControl1 = car1.GetComponent<CarControl>();//得到车一的控制器
        carControl2 = car2.GetComponent<CarControl>();//得到车二的控制器

        car1.GetComponent<Rigidbody>().mass = CarMass1;//赋值车的质量
        car2.GetComponent<Rigidbody>().mass = CarMass2;
        SmoothFollow mainCamera = maincamera.GetComponent<SmoothFollow>();
        mainCamera.target = car1.transform;
        Car1AI = car1.GetComponent<CarAIControl>();//得到两车的AI寻找
        Car2AI = car2.GetComponent<CarAIControl>();

        carControl1.TopSpeed = FirstSpeed;//设置车一的最大速度
        carControl2.TopSpeed = SeconedSpeed;//设置车二的最大速度
        a = car1.GetComponent<Damage>();
        b = car2.GetComponent<Damage>();
        
        if (CollectType == "正碰")
        {
            Car1AI.SetTarget(car2.transform);//设置两车的碰撞目标（在此设置为双方）
            Car2AI.SetTarget(car1.transform);
            Car2AI.m_Driving = true;
        }
        else if (CollectType == "同向")
        {
            Car1AI.SetTarget(Point3.transform);
            Car2AI.m_Driving = true;
            Car2AI.SetTarget(Point4.transform);
        }
        else if (CollectType == "逆向")
        {
            Car1AI.SetTarget(Point3.transform);
            Car2AI.m_Driving = true;
            Car2AI.SetTarget(Point4.transform);
        }
        else if (CollectType == "追尾")
        {
            Car1AI.SetTarget(car2.transform);
            Car2AI.SetTarget(Point3.transform);
            Car2AI.m_Driving = true;
        }
        Car1AI.m_Driving = true;

    }
}
